package com.dushibao.dao;

import com.dushibao.common.Constants;
import com.dushibao.entity.Article;
import com.dushibao.entity.User;
import com.dushibao.utils.DBUtils;
import com.dushibao.utils.Page;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Types;
import java.util.ArrayList;
import java.util.List;

public class ArticleDao {

    public Integer save(Article article) {

        try{
            String sql = "insert into dushibao_article(author,title,content) values(?,?,?)";

            Connection conn = DBUtils.getConnection();
            PreparedStatement stmt = conn.prepareStatement(sql);
            stmt.setString(1,article.getAuthor());
            stmt.setString(2,article.getTitle());
            stmt.setString(3,article.getContent());
            stmt.executeUpdate();
            return Constants.SUCCESS;
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }


    public int update(Article article)  {
        try{
            String sql = "update dushibao_article set author=?,title=?,content=? where id=?";
            Connection conn = DBUtils.getConnection();
            PreparedStatement stmt = conn.prepareStatement(sql);
            stmt.setString(1,article.getAuthor());
            stmt.setString(2,article.getTitle());
            stmt.setString(3,article.getContent());
            stmt.setLong(4,article.getId());

            stmt.executeUpdate();
            return Constants.SUCCESS;
        }catch (Exception e){
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    public Page page(Page page, String author, String title) {

        try{
            StringBuilder querySql = new StringBuilder("select id,addTime,author,title,content from dushibao_article where 1=1 ");
            if(author != null && author.length()>0){
                querySql.append(" and author  like '%"+author+"%'");
            }

            if(title != null && title.length()>0){
                querySql.append(" and title  like '%"+title+"%'");
            }

            querySql.append(" limit ?,? ");

            Connection conn = DBUtils.getConnection();

            PreparedStatement stmt = conn.prepareStatement(querySql.toString());
            stmt.setInt(1,page.getStart());
            stmt.setInt(2,page.getPageSize());

            ResultSet rs = stmt.executeQuery();

            List<Object> objs = new ArrayList<>();
            while(rs.next()){
                Article article = new Article();
                article.setId(rs.getLong("id"));
                article.setAddTime(rs.getTimestamp("addTime").toLocalDateTime());
                article.setAuthor(rs.getString("author"));
                article.setTitle(rs.getString("title"));
                article.setContent(rs.getString("content"));
                objs.add(article);
            }

            DBUtils.close(rs,stmt,conn);

            StringBuilder countSql = new StringBuilder("select count(*) as total from dushibao_article where 1=1 ");
            if(author != null && author.length()>0){
                querySql.append(" and author  like '%"+author+"%'");
            }

            if(title != null && title.length()>0){
                querySql.append(" and title  like '%"+title+"%'");
            }

            conn = DBUtils.getConnection();
            stmt = conn.prepareStatement(countSql.toString());

            rs = stmt.executeQuery();

            int total = 0;
            if(rs.next()){
                total = rs.getInt("total");
            }

            Page iPage = new Page();
            iPage.setCurrent(page.getCurrent());
            iPage.setPageSize(page.getPageSize());
            iPage.setData(objs);
            iPage.setPages(total % iPage.getPageSize()==0?total/ iPage.getPageSize():total/ iPage.getPageSize()+1);
            iPage.setTotal(total);

            return iPage;
        }catch (Exception e){
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }


    public Article getById(long id) {
        try{
            String sql = "select id,addTime,author,title,content from dushibao_article where id=? ";
            Connection conn = DBUtils.getConnection();
            PreparedStatement stmt = conn.prepareStatement(sql);
            stmt.setLong(1,id);
            ResultSet rs = stmt.executeQuery();
            Article article = new Article();

            if(rs.next()){
                article.setId(rs.getLong("id"));
                article.setAddTime(rs.getTimestamp("addTime").toLocalDateTime());
                article.setAuthor(rs.getString("author"));
                article.setTitle(rs.getString("title"));
                article.setContent(rs.getString("content"));
            }

            return article;
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }


    public Integer deleteById(List<Long> ids) {
        try{

            StringBuilder sql = new StringBuilder(" delete from dushibao_article where id in ( ");
            for (int i = 0; i < ids.size(); i++) {
                sql.append(ids.get(i));

                if(i<ids.size()-1){
                    sql.append(",");
                }

            }
            sql.append(")");

            Connection conn = DBUtils.getConnection();
            PreparedStatement stmt = conn.prepareStatement(sql.toString());
            stmt.executeUpdate();
            return Constants.SUCCESS;
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }
}
